﻿$(document).ready(function () {
    $("#themDeThi").click(function () {
        $("#fThemDeThi").dialog({
            buttons: [
               {
                   html: "<span class='glyphicon glyphicon-plus'></span> Thêm", class: "btn btn-primary", click: function () {
                       ThemDeThi();
                   }
               }

            ]
        });
        $("#fThemDeThi").dialog("open");
        return false;
    });

    $("#fThemDeThi").dialog({
        autoOpen: false,
        modal: true,
        minHeight: 170,
        minWidth: 200,
        buttons: [
           {
               html: "<span class='glyphicon glyphicon-plus'></span> Thêm", class: "btn btn-primary", click: function () {
                   ThemDeThi();
               }
           }

        ]
    });
    $("#fThemCHNhanh").dialog({
        autoOpen: false,
        modal: true,
        minHeight: 170,
        minWidth: 200,
        buttons: [
           {
               html: "<span class='glyphicon glyphicon-plus'></span> Thêm", class: "btn btn-primary", click: function () {
                   themNhanhCauHoi();
               }
           }

        ]
    });
});

function GetInfo(id) {
    $.ajax({
        type: "POST",
        url: "../Ajax/DeThiAjax.aspx",
        data: { Methods: "GetInfo", MaDe: id },
        success: function (result) {
            result = $.parseJSON(result);
            $("#txtMaDe").val(result.MaDe);
            $("#txtDeThi").val(result.TenDe);
            $("#txtThoiGianThi").val(result.ThoiGianThi);
            $("#txtThangDiem").val(result.ThangDiem);
            $("#txtChuThich").val(result.ChuThich);
            $("#fThemDeThi").dialog({
                buttons: [
               {
                   html: "<span class='glyphicon glyphicon-search'></span> Cập nhật", class: "btn btn-primary", click: function () {
                       CapNhat();
                   }
               }

                ]
            });
            $("#fThemDeThi").dialog("open");
        },
        error: function () {

        }
    });
}

function ThemDeThi() {
    var dethi = $("#txtDeThi").val();
    var thoigianthi = $("#txtThoiGianThi").val();
    var thangdiem = $("#txtThangDiem").val();
    var chuthich = $("#txtChuThich").val();
    var data = {};
    data.TenDe = dethi;
    data.ThoiGianThi = thoigianthi;
    data.ThangDiem = thangdiem;
    data.ChuThich = chuthich;
    $.ajax({
        type: "POST",
        url: "../Ajax/DeThiAjax.aspx",
        data: { Methods: "ThemDeThi", Data: JSON.stringify(data) },
        success: function (result) {
            result = $.parseJSON(result);
            if (result.Status == "Success") {
                __doPostBack('ctl00$MainContent$refresh', '');
                $("#fThemDeThi").dialog("close");
            }
            else {
                jAlert(result.Mess, "Thong bao");
            }
        },
        error: function () {

        }
    });
}

function CapNhat() {
    var made = $("#txtMaDe").val();
    var dethi = $("#txtDeThi").val();
    var thangdiem = $("#txtThangDiem").val();
    var chuthich = $("#txtChuThich").val();
    var thoigianthi = $("#txtThoiGianThi").val();
    
    var data = {};
    data.MaDe = made;
    data.TenDe = dethi;
    data.ThangDiem = thangdiem;
    data.ChuThich = chuthich;
    data.ThoiGianThi = thoigianthi;
    $.ajax({
        type: "POST",
        url: "../Ajax/DeThiAjax.aspx",
        data: { Methods: "CapNhat", Data: JSON.stringify(data) },
        success: function (result) {
            result = $.parseJSON(result);
            if (result.Status == "Success") {
                __doPostBack('ctl00$MainContent$refresh', '');
                $("#fThemDeThi").dialog("close");
            }
            else {
                jAlert(result.Mess, "Thong bao");
            }
        },
        error: function () {

        }
    });
}

function showAlert(MaDe) {
    jConfirm("Xác nhận xóa?", "Thông báo", function (iOK) {
        if (iOK) {
            xoaDeThi(MaDe);
        }
    });
    return false;
}

function xoaDeThi(MaDe) {
    $.ajax({
        type: "POST",
        url: "../Ajax/DeThiAjax.aspx",
        data: { Methods: "XoaDeThi", MaDeThi: MaDe },
        success: function (result) {
            result = $.parseJSON(result);
            if (result.Status == 'Success') {
                __doPostBack('ctl00$MainContent$refresh', '');
            } else {
                jAlert(result.Mess, "Thong bao");
            }
        },
        error: function () {

        }
    });
    return false;
}

function showNhanhCauHoi(MaDe) {
    $("#txtMaDeTN").val(MaDe);
    $("#fThemCHNhanh").dialog("open");
    return false;
}

function themNhanhCauHoi() {
    var dethi = $("#txtMaDeTN").val();
    var socauhoi = $("#txtSoCauHoi").val();
    var monthi = $("#ddlMonThi").val();
    var socaude = $("#txtSoCauDe").val();
    var socautrungbinh = $("#txtSoCauTrungBinh").val();
    var socaukho = $("#txtSoCauKho").val();
    if (socauhoi != (parseInt(socaude) + parseInt(socautrungbinh) + parseInt(socaukho))) {
        jAlert("Tổng số câu hỏi không bằng nhau!", "Thông báo");
        return;
    }
    $.ajax({
        type: "POST",
        url: "../Ajax/DeThiAjax.aspx",
        data: { Methods: "ThemNhanhCH", DeThi: dethi, SoCauHoi: socauhoi, MonThi: monthi, SoCauDe: socaude, SoCauTB: socautrungbinh, SoCauKho: socaukho },
        success: function (result) {
            result = $.parseJSON(result);
            if (result.Status == "Success") {
                __doPostBack('ctl00$MainContent$refresh', '');
                jAlert("Thêm câu hỏi thành công!","Thông báo");
                $("#fThemCHNhanh").dialog("close");
            }
            else {
                jAlert(result.Mess, "Thông báo");
            }
        },
        error: function () {

        }
    });
}